package thread;

import dao.FruitDao;
import domain.Fruit;
import domain.Manager;

import java.io.*;
import java.net.Socket;
import java.util.ArrayList;
import java.util.TreeSet;

public class SocketRunable implements Runnable {

    FruitDao dao = new FruitDao();
    ArrayList<Fruit> list = dao.findAllFruits();
    TreeSet<Fruit> sort = dao.sortByInventory();
    TreeSet<Fruit> fruitTreeSet = dao.sortByPrice();
    private Socket socket;

    public SocketRunable(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        try {
            //解析客户端的请求数据,来匹配对应的操作
            BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String s = br.readLine();
            System.out.println("本次客户端请求的数据为:" + s);
            String option = s.substring(0, 3);
            String data = s.substring(3);
            // String[] split = data.split(",");
            switch (option) {
                case "[0]":
                    login(data);
                    break;
                case "[1]":
                    //managerCheck();
                    exists(data);
                    break;
                case "[3]":
                    add(data);
                    break;
                case "[4]":
                    delete(data);
                case "[5]":
                    update(data);
                case "[6]":
                    check();
                    break;
            }
        } catch (IOException e) {

        }
    }

    private void login(String data) {
        String[] split = data.split(",");
        ArrayList<Manager> man = dao.login();
        boolean flag = false;
        for (Manager everyMan : man) {
            if (everyMan.getName().equals(split[0]) && everyMan.getPassWord().equals(split[1])) {
                flag = true;
            }
        }
        try {
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            if (flag) {
                bw.write("true");
            } else {
                bw.write("false");
            }
            bw.newLine();
            bw.flush();
            bw.close();
            socket.shutdownOutput();
        } catch (IOException e) {
        }

    }

    public int getIndex(String num) {
        return dao.getIndex(num);
    }

    public void inventory(String fnum, int num, int[] arr) {
        dao.inventory(fnum, num, arr);
    }

    public void checkByPrice() {
        System.out.println("编号" + "\t\t\t" + "名称" + "\t\t" + "价格" + "\t\t" + "库存" + "\t\t" + "单位");
        fruitTreeSet.addAll(list);
        for (Fruit f : fruitTreeSet) {
            System.out.println(f.getNum() + "\t\t" + f.getName() + "\t\t" + f.getPrice() + "\t\t\t" + f.getInventory() + "\t\t\t" + f.getUnit());
        }
    }

    //顾客查看水果信息时的默认方法,按库存排序
    public void check1() {
        //System.out.println("编号" + "\t\t\t" + "名称" + "\t\t" + "价格" + "\t\t" + "库存" + "\t\t" + "单位");
        for (Fruit f : sort) {
            System.out.println(f.getNum() + "\t\t" + f.getName() + "\t\t" + f.getPrice() + "\t\t\t" + f.getInventory() + "\t\t\t" + f.getUnit());
        }
    }

    public void check() {
        //System.out.println("编号" + "\t\t\t" + "名称" + "\t\t" + "价格" + "\t\t" + "库存" + "\t\t" + "单位");
        try {
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            for (Fruit f : list) {
                bw.write(f.getNum() + "\t\t" + f.getName() + "\t\t" + f.getPrice() + "\t\t\t" + f.getInventory() + "\t\t\t" + f.getUnit());
                bw.newLine();
                bw.flush();
            }
            bw.close();
            socket.shutdownOutput();
        } catch (IOException e) {

        }

    }

    //管理员查看水果信息时的方法,直接读取文件中的水果信息,不支持排序
    public void managerCheck() {
        try {
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            //bw.write("编号" + "\t\t\t" + "名称" + "\t\t" + "价格" + "\t\t" + "库存" + "\t\t" + "单位");
            //bw.newLine();
            for (Fruit f : list) {
                bw.write(f.getNum() + "\t\t" + f.getName() + "\t\t" + f.getPrice() + "\t\t\t" + f.getInventory() + "\t\t\t" + f.getUnit());
                bw.newLine();
            }
        } catch (IOException e) {

        }
        //bw.close();
        //socket.shutdownOutput();
        //socket.close();
    }

    public ArrayList<Fruit> findAllFruits() {
        return list;
    }

    private void exists(String data) throws IOException {
        //1. 指挥库管将系统中所有数据查出来
        /*
            stus的数据情况:
                第一种: 没有数据    {null,null,null,null,null}
                第二种: 有一些数据  {stu,stu,null,null,null}
                第三种: 装满了      {stu,stu,stu,stu,stu}
         */
        //Fruit[] stus = dao.findAllFruit();

        //2. 比对当前id是否存在
        //先假设要查的学号不存在
        boolean exists = false;

        for (int i = 0; i < list.size(); i++) {
            //遍历数组,取出每一个学生对象
            Fruit fru = list.get(i);
            if (data.equals(fru.getNum())) {
                //修改标记位true
                exists = true;
            }
        }
        //将结果通过socket写回给客户端
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        if (exists) {
            bw.write("true");
        } else {
            bw.write("false");
        }
        bw.newLine();
        bw.flush();

        //释放资源
        bw.close();
        socket.close();
    }

    public boolean isExist(String num) {
        boolean flag = false;
        for (Fruit fruit : list) {
            if (fruit.getNum().equals(num)) {
                flag = true;
            }
        }

        return flag;
    }

    private void add(String data) {
        try {
            String[] split = data.split(",");
            Fruit fru = new Fruit(split[0], split[1], Integer.parseInt(split[2]), Integer.parseInt(split[3]), split[4]);

            //2. 指挥库管做添加
            boolean res = dao.add(fru);
            //将结果通过socket写回给客户端
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            if (res) {
                bw.write("true");
            } else {
                bw.write("false");
            }
            bw.newLine();
            bw.flush();

            //释放资源
            bw.close();
            socket.close();
        } catch (Exception e) {

        }
    }

   /* public void add(String num, String name, int price, int inventory, String unit) {
        dao.add(num, name, price, inventory, unit);
    }*/

    public void delete(String data) {
        boolean res = dao.delete(data);
        try {
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            if (res) {
                bw.write("true");
            } else {
                bw.write("false");
            }
            bw.newLine();
            bw.flush();
        } catch (IOException e) {

        }
    }

    public void update(String data) {
        String[] split = data.split(",");
        Fruit fru = new Fruit(split[0], split[1], Integer.parseInt(split[2]), Integer.parseInt(split[3]), split[4]);

        //2. 指挥库管做添加
        boolean res = dao.update(split[0], fru);
        //将结果通过socket写回给客户端
        try {
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            if (res) {
                bw.write("true");
            } else {
                bw.write("false");
            }
            bw.newLine();
            bw.flush();

            //释放资源
            bw.close();
            socket.shutdownOutput();
        } catch (IOException e) {

        }

    }

    public int count(String name, int num) {
        int sum = 0;
        for (Fruit fruit : list) {
            if (name.equals(fruit.getName())) {
                sum += fruit.getPrice() * num;
            }
        }
        return sum;
    }
}